﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using HHCommon;

namespace HHWeb.SystemManage.Partners
{
    public partial class Dispose : Base
    {
        HHBLL.Partners B = new HHBLL.Partners();
        HHModel.Partners M = new HHModel.Partners();
        Help help = new Help();
        EncAndDec EncAndDec = new EncAndDec();
        //变量定义
        public string Name = string.Empty;
        public string ImgUrl = string.Empty;
        public string URL = "http://";
        public string PartnerType = string.Empty;
        public int Sort =0;
        public int Status =1;
        public string Creater = string.Empty;
        public string CreateTime = string.Empty;
        public string Command = "Add";//执行命令
        protected void Page_Load(object sender, EventArgs e)
        {
            string message = string.Empty;
            try
            {
                if (help.GetHttpMethodBool())
                {
                    Command = Request.Form["Command"].ToString();
                    switch (Command)
                    {
                        case "Add":
                            message = AddPartner();
                            break;
                        case "Update":
                            message = UpdatePartner();
                            break;
                        default:
                            break;
                    }
                    if (message.EndsWith("成功！"))
                        MessageBox.ShowAndRedirect(this.Page, message, "List.aspx");
                    else
                        MessageBox.ResponseScript(this.Page,message,"javasrcript:history.go(-1)");
                }
                else
                {
                    if (help.IFPageParameter())
                    {
                        int ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
                        M = B.GetModel(ID);
                        if (M != null)
                        {
                            Name = M.Name;
                            URL = M.URL;
                            ImgUrl = M.ImgUrl;
                            PartnerType = M.PartnerType;
                            FCKeditor2.Value = M.Remark;
                            Sort = Convert.ToInt32(M.Sort);
                            Status = Convert.ToInt32(M.Status);
                            Creater = M.Creater;
                            CreateTime = Convert.ToDateTime(M.CreateTime).ToString("yyyy-MM-dd HH:mm:ss");
                            Command = "Update";
                        }
                        else
                        {
                            MessageBox.ShowAndRedirect(this.Page, "该数据不存在！", "List.aspx");
                            return;
                        }
                    }
                    else
                    {
                        Creater = EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
                        CreateTime = Convert.ToDateTime(M.CreateTime).ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
            }
            catch
            {
                MessageBox.ResponseScript(this.Page, "操作错误，请联系管理员！", "javascript:history.go(-1)");
                return;
            }
        }

        #region //添加合作伙伴
        public string AddPartner()
        {
            M.Name = Request.Form["Name"].ToString();
            M.URL = Request.Form["URL"].ToString();
            #region //图片上传
            HttpPostedFile hpf =upload_img.PostedFile;
            string fileName = Path.GetFileName(hpf.FileName);
            string ex = Path.GetExtension(fileName);//获取文件后缀名
            if (string.IsNullOrEmpty(fileName))
                return "上传图片不能为空！";
            string SavaUrl=string.Empty;
            if (ex == ".gif" || ex == ".jpg" || ex == ".bmp" || ex == ".png")
            {
                SavaUrl = "../../MYFiles/PartnerImg/"+help.GetRandom()+ex;
                hpf.SaveAs(Server.MapPath(SavaUrl));//保存图片路经
            }
            else
                return "图片格式错误！";
            #endregion
            M.ImgUrl = SavaUrl;
            M.PartnerType = Request.Form["PartnerType"].ToString();
            M.Remark = FCKeditor2.Value;
            M.Sort = Convert.ToInt32(Request.Form["Sort"].ToString());
            M.Status = Convert.ToInt32(Request.Form["Status"].ToString());
            M.Creater = Request.Form["Creater"].ToString();
            M.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"]);
            if (B.Add(M) > 0)
                return "添加成功！";
            else
                return "添加失败！";
        }
        #endregion

        #region //修改合作伙伴
        public string UpdatePartner()
        {
            M.ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
            M.Name = Request.Form["Name"].ToString();
            M.URL = Request.Form["URL"].ToString();
            #region //图片上传
            HttpPostedFile hpf = upload_img.PostedFile;
            string fileName = Path.GetFileName(hpf.FileName);
            string ex = Path.GetExtension(fileName);//获取文件后缀名
            string SavaUrl = string.Empty;
            if (!string.IsNullOrEmpty(fileName))
            {
                if (ex == ".gif" || ex == ".jpg" || ex == ".bmp" || ex == ".png")
                {
                    SavaUrl = "../../MYFiles/PartnerImg/" + help.GetRandom() + ex;
                    if (System.IO.File.Exists(Server.MapPath(Request.Form["ImgUrl"].ToString())))
                        System.IO.File.Delete(Server.MapPath(Request.Form["ImgUrl"].ToString()));
                    hpf.SaveAs(Server.MapPath(SavaUrl));//保存图片路经
                }
                else
                    return "图片格式错误！";
                M.ImgUrl = SavaUrl;
            }
            else
                M.ImgUrl = Request.Form["ImgUrl"].ToString();
            #endregion
            M.PartnerType = Request.Form["PartnerType"].ToString();
            M.Remark = FCKeditor2.Value;
            M.Sort = Convert.ToInt32(Request.Form["Sort"].ToString());
            M.Status = Convert.ToInt32(Request.Form["Status"].ToString());
            M.Creater = Request.Form["Creater"].ToString();
            M.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"]);
            if (B.Update(M))
                return "修改成功！";
            else
                return "修改失败！";
        }
        #endregion
    }
}